package job;

import java.util.List;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import model.Param;
import play.db.jpa.JPA;
import play.jobs.Every;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
@OnApplicationStart
@Every("5min")
public class Returnfeemode extends Job{
	Logger log = LoggerFactory.getLogger(this.getClass());
	@Override
	public void	doJob(){
		//log.info("进入清分返款模式job-------------------");
		String sqlqf = "select q.netinsuranceid from  qf_stationinsurance q WHERE q.insurancestatus = 1 and q.returnfeemode IS NULL limit 0,300";
		try {
			Query query = JPA.em().createNativeQuery(sqlqf);
			List<String> qflist = query.getResultList();
			if (qflist!=null&&qflist.size()!=0) {
				String sqlre = "select t.netticketid,t.returnfeemode from ticketreturn t WHERE t.netticketid in (:qflist)";
				Query queryret = JPA.em().createNativeQuery(sqlre,Param.class);
				queryret.setParameter("qflist", qflist);
				List<Param> paramlist = queryret.getResultList();
				for (Param param : paramlist) {
					String qudate = "UPDATE qf_stationinsurance set returnfeemode =:returnfeemode WHERE netinsuranceid =:netinsuranceid";
					Query queryupdate = JPA.em().createNativeQuery(qudate);
					queryupdate.setParameter("returnfeemode", param.returnfeemode);
					queryupdate.setParameter("netinsuranceid", param.netticketid);
					queryupdate.executeUpdate();
				}
				JPA.em().getTransaction().commit();
			}
		} catch (Exception e) {
			log.error("操作数据库失败",e);
		}
		//log.info("出去清分返款模式job++++++++++++++++++++");
	}
	
	
	
}
